/*$(document).ready(function() {
 findlingshou(1);
 });*/

// 打开模态框
function opendMo() {
    initialVip();
    $('#myModala').modal('show');
}
function openTuihuo(id, tuiHuoDanhao, tuiVipName, tuiZhifu, wanglaidanwei) {
    shanchu = 0;// 置0删除表格的
    $("#tuiHuoDanhao").text(tuiHuoDanhao);
    $("#tuiVipName").text(tuiVipName);
    if (tuiZhifu == 0) {
        $("#tuiZhifu").text('现金支付');
    } else {
        $("#tuiZhifu").text('会员卡支付');
    }
    $("#chukudanhaoid").text(id);
    $("#wanglaidanwei").text(wanglaidanwei)
    $.ajax({
        url: path + '/chuku/sale/findByChukuid.do',
        data: {
            id: id
        },
        dataType: 'json',
        success: function (data) {
            if (data != null) {
                var str = ""
                for (var i = 0; i < data.length; i++) {
                    str += '<option>' + data[i].productCode + '</option>'
                }

                $("#tuohuoKuanhao").html(str);
                $('#tuohuoKuanhao').selectpicker('refresh');
            }
        },
        complete: function (XMLHttpRequest, status) { // 请求完成后最终执行参数

        }
    })
    initialTuohuo();
    $('#myModalTuihuo').modal('show')
}
// 删除某一行
function tuiRemove(id) {
    var submit = function (v, h, f) {
        if (v == 'ok') {
            $("#tuiHuos").bootstrapTable('removeByUniqueId', id);// 删除某一行经典!经典!
            $("#tuiHuos").bootstrapTable('refresh');
            $('#tuiHuos').bootstrapTable('resetView');
            jBox.tip('删除成功!', 'success');
            tuiYingtui();
        }
        return true; // close
    };
    $.jBox.confirm("确定要删除数据吗？", "提示", submit);
}
// 添加
function addTuihuo() {
    $.ajax({
        url: path + '/chuku/sale/findSales.do',
        data: {
            productCode: $("#tuohuoKuanhao").val().trim(),
            id: $("#chukudanhaoid").text()
        },
        dataType: 'json',
        success: function (data) {
            if (data != null) {
                $("#tuiHuos").bootstrapTable('refresh');
                var tuiHuos = $('#tuiHuos').bootstrapTable('getData');
                for (var i = 0; i < tuiHuos.length; i++) {
                    if (data.productCode == tuiHuos[i].productCode) {
                        if (Math.abs(tuiHuos[i].ketuishuliang) < Math.abs((tuiHuos[i].shuliang - 0)) + 1) {
                            jBox.tip("已超出退货数量，请核对数量!", 'info');
                            return;
                        }
                        $("#tuiHuos")
                            .bootstrapTable(
                                'updateRow',
                                {
                                    index: i,
                                    row: {
                                        shuliang: (tuiHuos[i].shuliang - 0) + 1
                                    }
                                });

                        tuiYingtui();
                        return;
                    }
                }
                $("#tuiHuos").bootstrapTable("append", {
                    productionDetailsId: data.productionDetailsId,
                    productCode: data.productCode,
                    productName: data.productName,
                    color: data.color,
                    size: data.size,
                    ketuishuliang: data.ketuishuliang,
                    shuliang: 1,
                    pinpai: data.pinpai,
                    price: data.price.toFixed(2),
                    danjia: data.danjia.toFixed(2)
                });
                tuiYingtui();
            }
        },
        complete: function (XMLHttpRequest, status) { // 请求完成后最终执行参数
        }
    })

}

// 计算应退金额
function tuiYingtui() {
    var tuiHuos = $('#tuiHuos').bootstrapTable('getData');
    var yingtui = 0;
    for (var i = 0; i < tuiHuos.length; i++) {
        yingtui += (tuiHuos[i].danjia * tuiHuos[i].shuliang)
    }
    var yingtuiqian = (yingtui - $("#moling").val()).toFixed(2)
    if (yingtuiqian < 0) {
        $("#yingtui").val("0.00")
    } else {
        $("#yingtui").val(yingtuiqian)
    }
}
// 零售退货
function addTuihuoChu() {
    var rows = $('#tuiHuos').bootstrapTable('getData');
    if (rows.length < 1) {
        jBox.tip("请添加退货商品!", 'success');
        return;
    }
    var a = '[';
    var zongshuliang = 0;
    var zongjia = 0;
    var chukudanhaoid = $("#chukudanhaoid").text();
    for (var i = 0; i < rows.length; i++) {
        zongshuliang += rows[i].shuliang;
        zongjia += (rows[i].shuliang * rows[i].danjia);
        if (i == rows.length - 1) {
            a += "{'shuliang':'" + rows[i].shuliang + "','productDetailsId':'"
                + rows[i].productionDetailsId + "','danjia':'" + rows[i].danjia + "'}]"
            break;
        }
        a += "{'shuliang':'" + rows[i].shuliang + "','productDetailsId':'"
            + rows[i].productionDetailsId + "','danjia':'" + rows[i].danjia + "'},"
    }

    var moling = $("#moling").val() - 0;
    if (($("#yingtui").val() - 0).toFixed(2) != (zongjia - moling).toFixed(2)) {
        jBox.tip("数据异常,请核对重新尝试", 'error');
        return;
    }
    $('#queRen').attr('disabled', true);
    var zhifu = 0;
    if ($("#tuiZhifu").text() == "现金支付") {
        zhifu = 0;
    } else {
        zhifu = 1;
    }

    $.ajax({
        url: path + '/chuku/sale/saveTuihuo.do',
        type: 'post',
        data: {
            wanglaidanwei: $("#wanglaidanwei").text(),
            a: a,
            danjuId: $("#chukudanhaoid").text(),
            zhifu: zhifu,
            fenlei: 1,
        },
        dataType: 'json',
        success: function (data) {
            if (data.code == 200) {
                findXiaoshou();
                jBox.tip("退货成功!", 'success');
                $('#myModalTuihuo').modal('hide')
                initial();
                $('#tuiHuos').bootstrapTable('removeAll');
                $("#yingtui").val("0.00");

            } else {
                $.jBox.messager(data.msg, "提示", 2000, {
                    width: 350,
                    height: 100,
                    icon: 'error',
                    showType: 'show'
                });
            }

        },
        complete: function (XMLHttpRequest, status) { // 请求完成后最终执行参数
            if (status == 'timeout') {// 超时,status还有success,error等值的情况
                ajaxTimeoutTest.abort();
                $.jBox.messager("请求超时!", "提示", 2000, {
                    width: 350,
                    height: 100,
                    icon: 'error',
                    showType: 'show'
                });
            }
            $('#queRen').attr('disabled', false);
        }

    })

}

// 充钱
function chongqian(vipczid) {
    $("#vipczid").text(vipczid);
    $('#myModal-2').modal('show');
}
function chongzhiqian() {
    var id = $("#vipczid").text();

    var chongzhiqian = $("#chongzhiqian").val();
    if (!(/^[1-9]\d*(\.\d+)?$/).test(chongzhiqian)) {
        $("#chongzhiqian").popover('show');
        return;
    } else {
        $("#chongzhiqian").popover('hide');
    }
    $("#czan").attr("disabled", "disabled");

    $.ajax({
        url: path + '/vip/addMoney.do',
        data: {
            id: id,
            balance: chongzhiqian
        },
        dataType: 'json',
        success: function (data) {
            if (data.code == 200) {
                $("#chongzhiqian").val(0);
                $('#myModal-2').modal('hide');
                jBox.tip("充值成功!", 'success');
                initialVip();
                printVip(id);
            } else {
                jBox.tip(data.msg, 'error');
            }
        },
        complete: function (XMLHttpRequest, status) { // 请求完成后最终执行参数
            if (status == 'timeout') {// 超时,status还有success,error等值的情况
                $.jBox.messager("请求超时!请刷新重试.....", "提示", 2000, {
                    width: 350,
                    height: 100,
                    icon: 'error',
                    showType: 'show'
                });
            }
            $("#chongzhiqian").val(0);
            // 清除内容
            $("#czan").removeAttr("disabled");
        }
    })

}

// 修改会员
function changevip(id, huiyuanname, phone, shengri, xingbie, vipzk) {
    $("#xiugai").css('display', 'block');
    if (id == null || id == '') {// 新增
        $("#chong").show();
        $("#huiyuanname").val("");
        $("#phone").val("");
        $("#huiyuanshengri").val("");
        $("input[name='optionsRadios']").eq(0).attr("checked", true);
        $("#vipzk").val("");
        $("#czje").val("");
        $("#vipid").text("");
    } else {// 修改
        $("#chong").hide();
        $("#huiyuanname").val(huiyuanname.trim());
        $("#huiyuanshengri").val(shengri.trim());
        $("#vipid").text(id);
        for (var i = 0; i < 1; i++) {
            var xb = $("input[name='optionsRadios']").eq(i).val();
            if (xingbie == xb) {
                $("input[name='optionsRadios']").eq(i).attr("checked", true);
                break;
            }
        }
        $("#phone").val(phone.trim());
        $("#vipzk").val(vipzk);
    }
}
// 保存vip
function savevip() {

    var huiyuanming = $("#huiyuanname").val();
    var phone = $("#phone").val();
    var shengri = $("#huiyuanshengri").val();
    var xingbie = $("#sex").val();
    var vipzk = $("#vipzk").val();
    var yzzk = /^(0.\d+|0|1)$/ // 判断是否为0-1的小数包括0
    if (!yzzk.test(vipzk)) {
        $("#vipzk").popover('show');
        return;
    } else {
        $("#vipzk").popover('hide');
    }

    var yzsr = /^((?:19|20)\d\d)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$/;
    if (huiyuanming.trim() == "") {
        $("#huiyuanname").popover('show');
        return;
    } else {
        $("#huiyuanname").popover('hide');
    }

    if (shengri.trim() == "" || (!yzsr.test(shengri))) {
        $("#huiyuanshengri").popover('show');
        return;
    } else {
        $("#huiyuanshengri").popover('hide');
    }


    var balance = $("#czje").val();
    var yzje = /^([1-9]\d*|0)(\.\d*[1-9])?$/
    if (!yzje.test(balance) && $("#vipid").text() == "") {
        $("#czje").popover('show');
        return;
    }
    else {
        $("#czje").popover('hide');
    }


    $("#huiyuansave").attr("disabled", "disabled");
    $.ajax({
        url: path + '/vip/save.do',
        data: {
            huiyuanming: huiyuanming.trim(),
            phone: phone.trim(),
            shengri: shengri,
            xingbie: xingbie.trim(),
            vipzk: vipzk,
            balance: balance,
            id: $("#vipid").text()
        },
        dataType: 'json',
        success: function (data) {
            if (data.code == 200) {
                initialVip();
                jBox.tip("保存会员成功!", 'success');
                $("#xiugai").css('display', 'none');
            } else {
                $.jBox.messager(data.msg, "提示", 2000, {
                    width: 350,
                    height: 100,
                    icon: 'error',
                    showType: 'show'
                });
            }
        },
        complete: function (XMLHttpRequest, status) { // 请求完成后最终执行参数
            if (status == 'timeout') {// 超时,status还有success,error等值的情况
                $.jBox.messager("请求超时.....", "提示", 2000, {
                    width: 350,
                    height: 100,
                    icon: 'error',
                    showType: 'show'
                });
            }
            // 清除内容
            $("#huiyuansave").removeAttr("disabled");
        }
    })
}

// 设置会员
function xuanze(huiyuanid, huiyuanming, zekou) {
    if (zekou == 0) {
        zekou = 1
    }
    $("#huiid").text(huiyuanid);
    $("#huiming").text(huiyuanming);
    $("#zk").text(zekou);
    // $("." + biaoji).find('.huiyuanzekou').val(zekou);
    $('#myModala').modal('hide');
    $("#zk").text(zekou);
    var sum = $("." + biaoji).find('.tid').length;
    for (var i = 0; i < sum; i++) {
        var sjzhekou = (zekou - 0).toFixed(2);
        $("." + biaoji).find('.zekou').eq(i).val(sjzhekou)
        zongshuliang = $("." + biaoji).find('.zekou').eq(i).val();
        jisuanzk($("." + biaoji).find('.zekou').eq(i));
    }
    $("#shangpinbianhao").focus();
    zhaoling();
}

function dakaichongzhi() {// 用来打开充值页面
    $('#myModal-5').modal('hide');
    $("#vipczid").text($("#huiid").text());
    $("#chongzhiqian").val("");// 清空里面的值
    $('#myModal-2').modal('show');
}
function opendzhifu() {
    if ($("#shishou").val() == 0 || $("#shishou").val() == ""
        || $("#shishou").val() == null) {
        $("#shishou").val($("#zongjiage").val());
        $("#zhaoling").val(0);
    }
    if ($("#huiid").text() != -1) {
        $.ajax({
            url: path + '/vip/id.do',
            type: 'post',
            data: {
                id: $("#huiid").text()
            },
            dataType: 'json',
            success: function (data) {
                var data = data.data
                var balance = data.balance;
                if (balance == null) {
                    balance = 0
                }
                $("#kayue").text(balance.toFixed(2));
                var zongjiage = $("#zongjiage").val() - 0;
                if (balance < zongjiage) {
                    $("#huiyuankazhifu").attr("disabled", true);
                    $("#tishiyu").show();
                } else {
                    $("#huiyuankazhifu").attr("disabled", false);
                    $("#tishiyu").hide();
                }
            }
        })
        $('#myModal-5').modal('show');
    } else {
        addChuKu()
    }
}
function moling() {
    var shishou = $("#shishou").val() - 0;
    var zongjiage = $("#zongjiage").val() - 0;
    var yzje = /^([1-9]\d*|0)(\.\d*[1-9])?$/;
    if (!yzje.test(zongjiage)) {
        $.jBox.tip('请正确输入应收金额!', 'error');
        return;
    }

    var sum = $("." + biaoji).find('.tid').length;
    var zongjiagebefore = 0;
    //计算出原来记得总价格
    for (var i = 0; i < sum; i++) {
        zongjiagebefore = ($("." + biaoji).find('.shijijiage').eq(i).val() - 0)
            * ($("." + biaoji).find('.shuliang').eq(i).val() - 0)
            + zongjiagebefore;
    }
    //求出抹零的钱并平摊到每款上面

    var shuliang = 0;
    for (var i = 0; i < sum; i++) {
        shuliang += $("." + biaoji).find('.shuliang').eq(i).val() - 0;
    }
    var moling = (zongjiagebefore - zongjiage) / shuliang;
    //计算折扣
    for (var i = 0; i < sum; i++) {
        $("." + biaoji).find('.shijijiage').eq(i).val(($("." + biaoji).find('.shijijiage').eq(i).val() - 0 - moling).toFixed(2));
        $("." + biaoji).find('.zekou').eq(i).val((($("." + biaoji).find('.shijijiage').eq(i).val() - 0) / ($("." + biaoji).parent().parent().parent().find(".jiage").eq(i).text() - 0)).toFixed(2))
    }


    jisuanzongjia();

    if (shishou != "") {
        if (shishou >= zongjiage) {
            $("#zhaoling").val(shishou - zongjiage);
        } else {
            $("#shishou").focus();
            $.jBox.tip('请正确输入实收金额!', 'error');
            return;
        }

    }
}
function addChuKu(tar) {
    var zhifu = '';
    var jiage = $('#zongjiage').val() - 0;
    var shishou = 0;
    if (tar != null && tar != '') {//会员卡支付
        zhifu = tar;
        shishou = jiage;
    } else {//现金支付
        zhifu = 0;
        shishou = $("#shishou").val();
    }

    if (jiage > shishou) {
        $.jBox.tip('请正确输入实收金额!', 'error');
        $("#shishou").focus();
        return;
    }
    var rows = $("." + biaoji).eq(1).find(".tid").length;

    var yzsl = /^\+?[0-9][0-9]*$/;// 判断是否为正整数
    var yzzk = /^(0.\d+|0|1)$/ // 判断是否为0-1的小数包括0
    var yzdj = /^[0-9]\d*(\.\d+)?$/ // 判断必须大于1的正数；

    if ($("#shishou").val().trim() == "") {
        $.jBox.tip('请正确输入实收金额!', 'error');
        return;
    }
    if (rows == 0) {
        $.jBox.tip('请先添加商品!', 'error');
        return;
    }
    var a = '[';
    for (var i = 0; i < rows; i++) {
        var zekou = $("." + biaoji).find('.zekou').eq(i).val() - 0;
        var shuliang = $("." + biaoji).find('.shuliang').eq(i).val() - 0;
        var shijijiage = ($("." + biaoji).find('.shijijiage').eq(i).val() - 0);
        if (!yzdj.test(shijijiage)) {
            $.jBox.tip('输入的实际价格不合法!', 'error');
            return;
        }
        if (!yzsl.test(shuliang)) {
            $.jBox.tip('输入的数量不合法!', 'error');
            return;
        }
        if (!yzzk.test(zekou)) {
            $.jBox.tip('输入的折扣不合法!', 'error');
            return;
        }
        if (i == rows - 1) {
            a += "{'danjia':" + shijijiage.toFixed(2) + ",'shuliang':" + shuliang + ",'productDetailsId':'" + $("." + biaoji).find('.tid').eq(i).text() + "'}]"
        } else {
            a += "{'danjia':" + shijijiage.toFixed(2) + ",'shuliang':" + shuliang + ",'productDetailsId':'" + $("." + biaoji).find('.tid').eq(i).text() + "'},"
        }
    }
    $("#save").attr("disabled", "disabled");
    $("#huiyuankazhifu").attr("disabled", "disabled");
    $("#xianjinzhifu").attr("disabled", "disabled");


    $.ajax({
        url: path + '/chuku/savelinghshou.do',
        type: 'post',
        data: {
            wanglaidanwei: $("#huiid").text(),// 等待输入
            fenlei: 0,// 区别退货还是出库
            a: a,// 拼接
            zhifu: zhifu,
            chukuren: $("#daogouyuan").val(),
            shishou: shishou
        },
        dataType: 'json',
        success: function (data) {
            if (data.code == 200) {
                $("." + biaoji).eq(1).find('.table-responsive').text("");
                $("." + biaoji).eq(1).find('.table-responsive2').text("");
                $.jBox.tip('销售成功', 'success');
                printer(data.data);
                $("#shishou").val("");
                $("#zhaoling").val("");
                $("#huiid").text("-1");
                $("#zk").text("1");
                $("#huiming").text("匿名");
                $("#tongyi").val(1)
                $("#zongjiage").val(0);
                $('#myModal-5').modal('hide');
                initial();
                findXiaoshou();
            } else {
                $.jBox.messager(data.msg, "提示", 3000, {
                    width: 350,
                    height: 100,
                    icon: 'error',
                    showType: 'show'
                });
            }
        },
        complete: function (XMLHttpRequest, status) { // 请求完成后最终执行参数
            if (status == 'timeout') {// 超时,status还有success,error等值的情况
                $.jBox.messager("请求超时.....，请检查网络后网络后重试!", "提示", 2000, {
                    width: 350,
                    height: 100,
                    icon: 'error',
                    showType: 'show'
                });
            }
            // 清除内容
            $("#save").removeAttr("disabled");
            $("#huiyuankazhifu").removeAttr("disabled");
            $("#xianjinzhifu").removeAttr("disabled");
        }

    })

}

function addcar() {
    var cpzbid = 0;
    $("#shishou").val("");
    $("#zhaoling").val("");
    $
        .ajax({
            url: path + '/production/findsale/productiondetailsid.do',// findProductController
            dataType: "json",
            data: {
                productCode: $("#shangpinbianhao").val().trim()
            },
            success: function (data) {
                $("#shangpinbianhao").val("");
                if (data.data != null && data.data != '') {
                    var data = data.data;
                    var lingshouzhekou = "";

                    if (data.zhekou < ($("#zk").text() - 0) && data.zhekou != 0) {
                        lingshouzhekou = data.zhekou;
                    } else {
                        lingshouzhekou = ($("#zk").text() - 0).toFixed(2);
                    }
                    var clas = "." + biaoji
                    var sum = $(clas).eq(1).find(".tid").length;
                    if (sum != 0) {
                        for (var i = 0; i < sum; i++) {
                            if (data.id == $(clas).find('.tid').eq(i)
                                    .text()) {
                                var shuangliang = $(clas).find('.tid')
                                        .eq(i).parent().next().find(
                                            ".shuliang").val() - 0;
                                $(clas).find('.tid').eq(i).parent().next()
                                    .find(".shuliang").val(
                                    shuangliang + 1);

                                var xiaoji = $(clas).find('.tid').eq(i)
                                        .parent().next().find(".shuliang")
                                        .val() - 0;
                                var mingpaijia = $(clas).find('.tid').eq(i)
                                        .parent().find(".jiage").val() - 0;
                                var zekou = $(clas).find('.tid').eq(i)
                                        .parent().next().find(".zekou")
                                        .val() - 0
                                $(".xiaoji").eq(i).text(
                                    (xiaoji * mingpaijia * zekou)
                                        .toFixed(2));
                                jisuanzongjia();
                                return;
                            } else {
                                var str = '';
                                str += '<div class="showMingxi" data-trigger="hover" rel="popover" data-content="'
                                    + "商品名称:"
                                    + data.productName
                                    + '" data-placement="bottom">'
                                str += '<table class="table">'
                                str += '<tbody>'


                                str += '<tr style="font-weight:700">'
                                str += '<td style="display:none" class="tid">'
                                    + data.id
                                    + '</td><td><a style="border:0;color:#F0AD4E" onclick="removed(this)" class="btn btn-default  glyphicon glyphicon-minus" href="#" role="button"></a></td>'
                                str += '<td style="max-width: 70px;" align="left">'
                                    + data.product + '</td>'
                                str += '<td>铭牌价:</td>'
                                str += '<td class="jiage">'
                                    + data.price.toFixed(2)
                                    + '</td>'
                                str += '<td>' + data.color + '</td>'
                                str += '<td>' + data.size + '</td>'
                                str += '</tr>'
                                str += '<tr style="background-color:#E8E8E8;">'
                                str += '<td >数量:</td>'
                                str += '<td ><input   class="form-control shuliang" onchange="jisuan(this)" style="max-width:50px; border:none" value="1"></td>'
                                str += '<td >折扣:<span class="lingshouzk" hidden>'
                                    + data.zhekou + '</span></td>'
                                str += '<td><input   onchange="jisuanzk(this)" class="form-control zekou" style="max-width: 70px; border:none" value="'
                                    + lingshouzhekou + '"></td>'
                                str += '<td>实际单价:</td>'
                                str += '<td style="width:90px"><input   onchange="jisuandj(this)" class="form-control shijijiage" style="max-width:80px; border:none" value="'
                                    + (data.price * lingshouzhekou)
                                        .toFixed(2) + '"></td>'
                                str += '<td style="display:none"  class="mingpaijia">'
                                    + data.price + '</td> </tr>'
                                str += '</tbody>'
                                str += '</table>';

                            }
                        }
                    } else {
                        var str = '';
                        str += '<div class="showMingxi" data-trigger="hover" rel="popover" data-content="'
                            + "商品名称:"
                            + data.productName
                            + '" data-placement="bottom">'
                        str += '<table class="table">'
                        str += '<tbody>'
                        str += '<tr style="font-weight:700">'
                        str += '<td style="display:none" class="tid">'
                            + data.id
                            + '</td><td><a style="border:0;color:#F0AD4E" onclick="removed(this)" class="btn btn-default  glyphicon glyphicon-minus" href="#" role="button"></a></td>'
                        str += '<td  style="max-width: 70px;" >'
                            + data.product + '</td>'
                        str += '<td>铭牌价:</td>'
                        str += '<td class="jiage">'
                            + data.price.toFixed(2) + '</td>'
                        str += '<td>' + data.color + '</td>'
                        str += '<td>' + data.size + '</td>'
                        str += '</tr>'
                        str += '<tr style="background-color:#E8E8E8;">'
                        str += '<td >数量:</td>'
                        str += '<td><input  onchange="jisuan(this)" class="form-control shuliang" style="max-width: 70px; border:none" value="1"></td>'
                        str += '<td >折扣:<span class="lingshouzk" hidden>'
                            + data.zhekou + '</span></td>';

                        str += '<td><input   onchange="jisuanzk(this)" class="form-control zekou" style="max-width: 70px; border:none" value="'
                            + lingshouzhekou + '"></td>'
                        str += '<td>实际单价:</td>'
                        str += '<td style="width:90px"><input onchange="jisuandj(this)" class="form-control shijijiage" style="max-width: 80px; border:none" value="'
                            + (data.price * lingshouzhekou).toFixed(2)
                            + '"></td>'
                        str += '<td style="display:none"  class="mingpaijia">'
                            + data.price + '</td> </tr>'
                        str += '</table>';
                        var str2 = "";
                        str2 += '<table class="table" >'
                        str2 += '<tr>'
                        str2 += '<td align="left"><b style="color: #424242;font-size: 23px">总数量(/件)：<span class="zongjianshu">1</span></b></td>'

                        str2 += '<td align="right" ><b style="color:  #424242;font-size: 23px">总计：</b></td>'
                        str2 += '<td align="right" >  <b style="color: #424242;font-size: 23px"><span  class="zongjiage">'
                            + (data.price * $("#tongyi").val())
                                .toFixed(2)
                            + '</span></b></td></tr></table>'

                    }

                    $(clas).eq(1).find('.table-responsive').append(str);
                    if ($(clas).eq(1).find('.table-responsive2').text()
                            .trim() == "") {
                        $(clas).eq(1).find('.table-responsive2').append(
                            str2);
                    }
                    jisuanzongjia();
                    $(".showMingxi").popover();
                } else {
                    $.jBox.tip('输入的商品编号不存在!请先入库该商品', 'error');
                }
            }
        })

}


function jisuandj(tar) {
    var yzdj = /^[0-9]\d*(\.\d+)?$/ // 判断必须大于1的正数；
    var yzzk = /^(0.\d+|0|1)$/ // 判断是否为0-1的小数包括0
    var mingpaijia = $(tar).parent().parent().parent().find(".mingpaijia")
            .text() - 0;
    var sjdanjia = $(tar).val() - 0;
    if (!yzdj.test(sjdanjia)) {
        $.jBox.tip('输入的单价不合法!', 'error');
        return;
    }
    $(tar).parent().parent().parent().find(".zekou").val(
        (sjdanjia / mingpaijia).toFixed(2));
    var zekou = $(tar).parent().parent().parent().find(".zekou").val();
    if (!yzzk.test(zekou)) {
        $.jBox.tip('输入的单价不合法!', 'error');
        return;
    }
    jisuanzongjia();
    jisuan();
    zhaoling();
    // $("#shishou").focus();
}

// jisuanzk
function jisuanzk(tar) {
    var yzzk = /^(0.\d+|0|1)$/ // 判断是否为0-1的小数包括0
    var mingpaijia = $(tar).parent().parent().parent().find(".mingpaijia")
            .text() - 0;
    var zekou = $(tar).val() - 0;
    if (!yzzk.test(zekou)) {
        $.jBox.tip('输入的折扣不合法!', 'error');
        return;
    }
    $(tar).parent().parent().parent().find(".shijijiage").val(
        (mingpaijia * zekou).toFixed(2));
    jisuanzongjia();
    jisuan();
    zhaoling();
}

$("#shishou").keyup(function (event) {
    if (event.code == 13) {
        zhaoling();
    }
});

// 计算总价格
function jisuan(tar) {
    var yzsl = /^\+?[0-9][0-9]*$/;// 判断是否为正整数\
    if (tar != null) {
        var shuliang = $(tar).val();
        if (!yzsl.test(shuliang)) {
            $.jBox.tip('输入的数量不合法!', 'error');
            return;
        }
    }

    jisuanzongjia();
    zhaoling();
    // $("#shishou").focus();
}
function removed(tar) {
    var submit = function (v, h, f) {
        if (v == 'ok') {
            $(tar).parent().parent().parent().parent().parent().popover(
                'destroy');
            $(tar).parent().parent().parent().parent().parent().remove();
            jisuanzongjia();
            jBox.tip('删除成功!', 'success');
            tuiYingtui();
        }
        return true; // close
    };
    $.jBox.confirm("确定要删除吗？", "提示", submit);
}

$("#shishou").keyup(function (event) {
    if (event.code == 13) {
        zhaoling();
    }
});

// 计算找零
function zhaoling() {
    if ($("#shishou").val() == "") {
        return;
    }
    var shishou = $("#shishou").val().trim() - 0;
    var zongresult = $('#zongjiage').val() - 0;
    var result = shishou - zongresult
    if (result < 0) {
        $("#zhaoling").val("");
        $.jBox.tip('输入的实收价格不合法!', 'error');
        return;
    } else {
        $("#zhaoling").val(result.toFixed(2));
        $("#save").focus();
        return;
    }
}

function jisuanzongjia() {
    var sum = $("." + biaoji).find('.tid').length;
    var zongshuliang = 0;
    var zongjiage = 0;
    for (var i = 0; i < sum; i++) {
        zongshuliang = $("." + biaoji).find('.shuliang').eq(i).val() - 0
            + zongshuliang;
        zongjiage = ($("." + biaoji).find('.shijijiage').eq(i).val() - 0)
            * ($("." + biaoji).find('.shuliang').eq(i).val() - 0)
            + zongjiage;
    }

    $("." + biaoji).eq(1).find('.zongjiage').text(zongjiage.toFixed(2));
    $("." + biaoji).eq(1).find('.zongjianshu').text(zongshuliang);
    $('#zongjiage').val((zongjiage).toFixed(2));

}


function tongyizekou() {
    var vipzekou = $("#tongyi").val() - 0;
    var yzzk = /^(0.\d+|0|1)$/ // 判断是否为0-1的小数包括0
    if (!yzzk.test(vipzekou)) {
        $.jBox.tip('输入的折扣不合法!', 'error');
        return;
    }
    vipzekou = vipzekou.toFixed(2);
    var sum = $("." + biaoji).find('.tid').length;
    for (var i = 0; i < sum; i++) {
        zongshuliang = $("." + biaoji).find('.zekou').eq(i).val(vipzekou);
        jisuanzk($("." + biaoji).find('.zekou').eq(i));
    }
    $("#shishou").focus();
    zhaoling();
}

// 格式化时间
Date.prototype.Format = function (fmt) { // author: meizz
    var o = {
        "M+": this.getMonth() + 1, // 月份
        "d+": this.getDate(), // 日
        "h+": this.getHours(), // 小时
        "m+": this.getMinutes(), // 分
        "s+": this.getSeconds(), // 秒
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "S": this.getMilliseconds()
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "")
            .substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k])
                : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

function printer(dayinid) {
    $.ajax({
        url: path + '/chuku/salexiaopiao.do',
        data: {
            id: dayinid
        },
        dataType: 'json',
        success: function (data) {
            if (data.code == 200) {
                var data = data.data;
                $('#body tbody').empty()
                $('#heard').empty()
                $('#body tfoot').empty()
                var heji = 0;
                var zongshuliang = 0;
                var zhifu = '';
                if (data[0].zhifu == 0) {
                    zhifu = '现金支付'
                } else {
                    zhifu = '会员卡支付'
                }
                for (var i = 0; i < data.length; i++) {
                    heji += (((data[i].danjia - 0) * Math.abs(data[i].shuliang))
                        .toFixed(2) - 0);
                    zongshuliang += Math.abs(data[i].shuliang);
                    $('#body tbody')
                        .append(
                            "<tr><td>"
                            + data[i].productName
                            + "</td><td style='width: 50px' align='center'>"
                            + Math.abs(data[i].shuliang)
                            + "</td> <td style='width: 50px'>"
                            + data[i].price.toFixed(2)
                            + "</td><td style='width: 50px'>"
                            + (data[i].price
                                .toFixed(2) * Math.abs(data[i].shuliang))
                                .toFixed(2)
                            + "</td></tr><tr ><td style='width: 110px;' colspan='2'>折扣:"
                            + (data[i].danjia / data[i].price).toFixed(2)
                            + "</td><td colspan='2'>折后价:"
                            + (data[i].danjia * Math.abs(data[i].shuliang))
                                .toFixed(2)
                            + "</td></tr>")
                }
                heji = (heji - (data[0].moling - 0)).toFixed(2) - 0;
                $('#heard')
                    .append(
                        "<tr><td align='center'style='font-family:DFKai-SB'><h2>"
                        + '苏格马可'
                        + "</h2> </td> </tr> <tr><td style='font-size: 1px;'>  流水号："
                        + data[0].chukudanhao
                        + "</td></tr>"
                        + "<tr><td style='font-size: 1px;'>销售员："
                        + data[0].zhidanren
                        + "</td></tr><tr><td style='font-size: 1px;'>会员："
                        + data[0].huiyuanming
                        + "</td></tr><tr> <td style='font-size: 1px;'>时间："
                        + new Date()
                            .Format("yyyy-MM-dd hh:mm:ss")
                        + "</td></tr><tr><td><div style='width: 100%; border-top: 1px dashed black;margin-top:8px;margin-bottom: 8px'></div> </td></tr>")
                var sta = ""
                sta += "<tr> <td colspan='4'><div style='width: 100%; border-top: 1px dashed black; margin-top:8px;margin-bottom: 8px'></div></td> </tr>"
                    + "<tr><td colspan='3'>总数</td><td style='width: 60px' align='left'>"
                    + zongshuliang
                    + "</td></tr><tr>"
                    + "<tr><td colspan='3'>合计</td><td style='width: 60px' align='left'>"
                    + heji.toFixed(2)
                    + "</tr></td><tr><td colspan='3'>实收</td><td style='width: 60px' align='left'>"
                    + (data[0].shishou).toFixed(2)
                    + "</td></tr><tr><td colspan='4'><div style='width: 100%; border-top: 1px solid ; margin-top:8px;margin-bottom: 8px' ></div> </td></tr>"
                    + "<tr><td colspan='3'>付款方式</td><td style='width: 120px' align='left' >"
                    + zhifu + "</td></tr>"
                if (zhifu == "现金支付") {
                    sta += "<tr><td colspan='3'>实付</td><td style='width: 60px' align='left' >"
                        + data[0].shishou.toFixed(2)
                        + "</td></tr>"
                        + "<tr><td colspan='3'>找零</td><td style='width:60px' align='left'>"
                        + (parseFloat(data[0].shishou) - parseFloat(heji)).toFixed(2) + "</td></tr>"
                } else {
                    sta += "<tr><td colspan='3'>会员卡余额</td><td style='width: 60px' align='left' >"
                        + ((data[0].balance - 0) + (heji.toFixed(2) - 0)).toFixed(2)
                        + "</td></tr>"
                        + "<tr><td colspan='3'>支付</td><td style='width:60px' align='left'>"
                        + (heji.toFixed(2) * -1.00).toFixed(2) + "</td></tr>"
                        + "<tr><td colspan='3'>交易后余额</td><td style='width:60px' align='left'>"
                        + data[0].balance.toFixed(2) + "</td></tr>"
                }
                $('#body tfoot').append(sta)
                $("#divOP").jqprint({
                    debug: false, // 如果是true则可以显示iframe查看效果
                    // （iframe默认高和宽都很小，可以再源码中调大），默认是false
                    importCSS: true, // true表示引进原来的页面的css，默认是true。
                    // （如果是true，先会找$("link[media=print]")，若没有会去找$("link")中的css文件）
                    printContainer: true, // 表示如果原来选择的对象必须被纳入打印
                    // （注意：设置为false可能会打破你的CSS规则）。
                    operaSupport: true
                    // 表示如果插件也必须支持歌opera浏览器，在这种情况下，
                    // 它提供了建立一个临时的打印选项卡。默认是true
                });
            } else {
                $.jBox.messager(data.msg, "提示", 2000, {
                    width: 350,
                    height: 100,
                    icon: 'error',
                    showType: 'show'
                });
            }
        }
    })

}
// 打印

function printVip(id) {
    $
        .ajax({
            url: path + '/vip/findczjl.do',
            data: {
                id: id
            },
            dataType: 'json',
            success: function (data) {
                var str = "";
                $('#vipses tbody').empty()
                str += "<tr><td align='center' colspan='2'><h5>"
                    + "会员充值小票"
                    + "</h5> </td> </tr><tr><td style='width: 50px' align='left''>操作员:<span>"
                    + data.data.chongzhiren
                    + "</span>"
                    + "</td></tr><tr><td style='width: 50px' align='left'>会员名:<span >"
                    + data.data.huiyuanming
                    + "</span></td></tr><tr><td style='width: 50px' align='left'>手机号码:<span style='width: 50px' align='left'>"
                    + data.data.phone
                    + "</span></td></tr><tr><td style='width: 50px' align='left'>充值时间:<span style='width: 50px' align='left'>"
                    + data.data.createTime
                    + "</span></td></tr><tr><td style='width: 50px' align='left'>原余额:<span style='width: 50px' align='left'>"
                    + ((data.data.balance - 0) - (data.data.czmoney - 0)).toFixed(2)
                    + "</span></td></tr><tr><td style='width: 50px' align='left'>充值金额:<span style='width: 50px' align='left'>"
                    + (data.data.czmoney - 0).toFixed(2)
                    + "</span></td></tr><tr><td style='width: 50px' align='left'>现余额:<span style='width: 50px' align='left'>"
                    + (data.data.balance - 0).toFixed(2)
                    + "</span></td></tr>";
                $('#vipses tbody').append(str)
                $("#divVip").jqprint({
                    debug: false, // 如果是true则可以显示iframe查看效果
                    // （iframe默认高和宽都很小，可以再源码中调大），默认是false
                    importCSS: true, // true表示引进原来的页面的css，默认是true。
                    // （如果是true，先会找$("link[media=print]")，若没有会去找$("link")中的css文件）
                    printContainer: true, // 表示如果原来选择的对象必须被纳入打印
                    // （注意：设置为false可能会打破你的CSS规则）。
                    operaSupport: true
                    // 表示如果插件也必须支持歌opera浏览器，在这种情况下，
                    // 它提供了建立一个临时的打印选项卡。默认是true
                });
            }
        })

}
